package org.paysim.order.service.impl;

import java.util.List;

import org.paysim.constant.OrderStatus;
import org.paysim.entity.OrderTable;
import org.paysim.exception.InvalidRequestException;
import org.paysim.mapper.OrderTableMapper;
import org.paysim.order.model.OrderCreateRequest;
import org.paysim.order.model.OrderDetailResponseData;
import org.paysim.order.model.OrderResponseData;
import org.paysim.order.service.IOrderService;
import org.paysim.util.OrderMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import jakarta.validation.Valid;
import jakarta.validation.constraints.NotNull;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author jiajin
 * @since 2025-04-27
 */
@Service
public class OrderServiceImpl extends ServiceImpl<OrderTableMapper, OrderTable> implements IOrderService {
    @Autowired
    private OrderMapper orderMapper;

    @Override
    public OrderResponseData createOrder(@Valid OrderCreateRequest orderCreateRequest) {
        OrderTable order = convertTable(orderCreateRequest);
        order.setorderStatus(OrderStatus.UNPAID.getStatus());
        save(order);
        return convertOrderResponse(order);
    }

    public OrderTable convertTable(OrderCreateRequest orderCreateRequest) {
        return orderMapper.orderToTable(orderCreateRequest);
    }

    public OrderResponseData convertOrderResponse(OrderTable orderTable) {
        return orderMapper.orderToResponseData(orderTable);
    }

    @Override
    public List<OrderDetailResponseData> getOrderList(@NotNull @Valid String userId) {
        QueryWrapper<OrderTable> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", userId).ne("order_status", OrderStatus.CANCELLED.getStatus());
        List<OrderTable> ordeList = list(wrapper);
        return convertOrderList(ordeList);
    }

    private List<OrderDetailResponseData> convertOrderList(List<OrderTable> ordeList) {
        return orderMapper.orderToOrdeList(ordeList);
    }

    @Override
    public void updateOrderStatus(String orderId, String orderStatus) {
        // TODO Auto-generated method stub
        UpdateWrapper<OrderTable> wrapper = new UpdateWrapper<>();
        // 设置更新条件
        wrapper.eq("order_id", orderId);
        // 设置要更新的字段和值
        wrapper.set("order_status", orderStatus);
        update(wrapper);
    }

    @Override
    public String queryOrderStatus(String orderId) {
        QueryWrapper<OrderTable> wrapper = new QueryWrapper<>();
        wrapper.eq("order_id", orderId);
        List<OrderTable> ordeList = list(wrapper);
        if (CollectionUtils.isEmpty(ordeList)) {
            throw new InvalidRequestException("orderId is invalid");
        }
        return ordeList.get(0).getorderStatus();
    }
}
